package com.zhiyou.servlet.user;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhiyou.dao.UserDao;
import com.zhiyou.daoImpl.UserDaoImps;
import com.zhiyou.model.User;
import com.zhiyou.service.UserService;
import com.zhiyou.serviceImpl.UserServiceImpl;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		UserService us = new UserServiceImpl();
		int a = us.login(username, password);
		if(a == 0){
			request.setAttribute("msg", "你的账户不存在");
			request.getRequestDispatcher("index.jsp").forward(request, response);
		
		}else if(a == 1){
			request.setAttribute("msg", "你输入的的密码错误");
			request.getRequestDispatcher("index.jsp").forward(request, response);
		
		}else {
			UserDao uDao = new UserDaoImps();
			User user = uDao.findByusernameAndpassword(username, password);
			request.getSession().setAttribute("user", user);
			response.sendRedirect("user.jsp");
		}
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
